<?PHP
header("Content-type: text/html; charset=utf-8");
session_start();
include_once "../public/DB_Oracle.php";
$Db_sql=new DB_Oracle();
/**
停止报警功能；
  */
if(isset($_POST['speech'])){
	$admin=$_SESSION['name'];
	for($i=0;$i<3;++$i){
	   if($i==0){
	$sql="insert into ala_message
		  (ala_id,ala_operator,ala_content,ala_time)
    values
      (ALA_MESSAGE_ID.nextval,'".$admin."','".$_POST['speech']."',sysdate)";
	$Db_sql->fetch_one_array($sql);
	$sql="select max(t.ala_id) max from ala_message t";
	$max_id=$Db_sql->fetch_one_array($sql);
    }
	sleep(3);
	$sql="select * from ala_message oi where oi.send_flag=1 and oi.ala_id =".$max_id['MAX']."";
	$row=$Db_sql->fetch_all_array($sql);
	if(count($row)>0){
		echo "T";
	   break;
	   }
	}
	exit();
	}


if(isset($_GET['index']))
{$sql="update sos_message t set t.proc_flag=1,t.proce_time=sysdate where t.sos_id=".$_GET['index'];
  $Db_sql->query($sql);
	exit();
	}
/**
显示报警搜索到的的数据；
  */
if(isset($_GET['lon'])&&isset($_GET['lat']))
{
	$lonlat_100=111.31955;  //经纬图一分除以1000的值 单位M;
	$lonlat=ceil($_GET['leng']/$lonlat_100)+1;
	$lonlat*=0.001;
	date_default_timezone_set('PRC');
	$time=date("Y-m-d H:i:s",mktime()-120);
	$time1=date("Y-m-d H:i:s",mktime());
	$lonlat_arr=array($_GET['lon']-$lonlat,$_GET['lon']+$lonlat,$_GET['lat']-$lonlat,$_GET['lat']+$lonlat);
$sql="select t.loc_index,t.device_id,t.longitude,t.latitude,t.recv_time,t.speed,t.angle,t.height 
from (select * from loc_vehicle_0101  where recv_time>to_date('".$time."','yyyy-mm-dd hh24:mi:ss') and recv_time<to_date('".$time1."','yyyy-mm-dd hh24:mi:ss')) t,
(select t.device_id imei,max(t.recv_time) time 
from loc_vehicle_0101 t  where t.recv_time>to_date('".$time."','yyyy-mm-dd hh24:mi:ss') and t.recv_time<to_date('".$time1."','yyyy-mm-dd hh24:mi:ss') group by t.device_id) t1
where t.device_id=t1.imei
  and t.recv_time=t1.time
  and to_number(t.longitude)>".$lonlat_arr[0]."
  and to_number(t.longitude)<".$lonlat_arr[1]."
  and to_number(t.latitude)>".$lonlat_arr[2]."
  and to_number(t.latitude)<".$lonlat_arr[3]."";
$data=$Db_sql->fetch_all_array($sql);
for($i=0;$i<count($data);++$i){
	$sql="select t.device_id,t.vehicle_name,t.license_plate,t1.department_name,t2.vehicle_type_name,t.sim from
   (select * from dep_vehicle ) t,
   (select * from dep_department) t1,
   (select * from dep_vehicle_type) t2
    where t.department_index=t1.department_index
    and t.vehicle_type_index=t2.vehicle_type_index
    and t.device_id='".$data[$i]['DEVICE_ID']."'";
	$data_a=$Db_sql->fetch_all_array($sql);
	if(count($data_a)>0){
		$data[$i]=array_merge($data[$i],$data_a[0]);

	echo "[".json_encode($data[$i])."]";
		}
	}


  $sql="select t.loc_index,t.device_id,t.longitude,t.longitude,t.latitude,t.recv_time,t.speed,t.angle,t.height from
(select * from loc_personnel_0101 where recv_time>to_date('".$time."','yyyy-mm-dd hh24:mi:ss') and recv_time<to_date('".$time1."','yyyy-mm-dd hh24:mi:ss')) t,
(select t.device_id imei,max(t.recv_time) time from loc_personnel_0101 t  where t.recv_time>to_date('".$time."','yyyy-mm-dd hh24:mi:ss') and t.recv_time<to_date('".$time1."','yyyy-mm-dd hh24:mi:ss') group by t.device_id) t1
where t.device_id=t1.imei
  and t.recv_time=t1.time
  and to_number(t.longitude)>".$lonlat_arr[0]."
  and to_number(t.longitude)<".$lonlat_arr[1]."
  and to_number(t.latitude)>".$lonlat_arr[2]."
  and to_number(t.latitude)<".$lonlat_arr[3]."";
$data=$Db_sql->fetch_all_array($sql);
for($i=0;$i<count($data);++$i){
	$sql="select t.device_id,t.personnel_name,t.personnel_sn,t1.department_name,t.sim
from (select * from dep_personnel ) t,
(select * from dep_department) t1
 where t.department_index=t1.department_index and t.device_id='".$data[$i]['DEVICE_ID']."'";
	$data_a=$Db_sql->fetch_all_array($sql);
	if(count($data_a)>0){
	$data[$i]=array_merge($data[$i],$data_a[0]);
	echo "[".json_encode($data[$i])."]";
		}
	}
	exit();
}



/*------------------------------------报警查询功能-----------------------------------*/
$sql="select * from sos_message t where t.proc_flag=0  order by t.sos_time desc";
$data=$Db_sql->fetch_all_array($sql);
	if(count($data)!=0){
		$sql="select * from dep_vehicle t where t.device_id='".$data[0]['SOS_IMEI']."'";
        $data1=$Db_sql->fetch_all_array($sql);
	if(count($data1)!=0){
		$sql="select t.device_id,t.vehicle_name,t.license_plate,t.sim,t1.department_name
from dep_vehicle t,dep_department t1 where t.department_index=t1.department_index and t.device_id='".$data[0]['SOS_IMEI']."'";
		$data_name=$Db_sql->fetch_all_array($sql);
	  $sql="select to_char(c.recv_time,'yyyy-mm-dd hh24:mi:ss') recv_time,c.longitude,c.latitude from(select * from loc_vehicle_0101 t where t.device_id='".$data[0]['SOS_IMEI']."' and t.recv_time<sysdate order by t.recv_time DESC) c where rownum = 1";
		$data_name1=$Db_sql->fetch_all_array($sql);
		echo $data[0]['SOS_ID']."_".$data_name[0]['SIM']."_".$data_name[0]['DEPARTMENT_NAME']."_".$data_name[0]['LICENSE_PLATE']."_".$data_name1[0]['RECV_TIME']."_".$data_name1[0]['LONGITUDE']."_".$data_name1[0]['LATITUDE']."_".$data_name[0]['SIM'];
	}else{
		exit();
		}
	}
	else{
		exit();
		}
?>